package com.umbrella.worker.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.umbrella.worker.dao.WAccountRecordMapper;
import com.umbrella.worker.dao.WOrderMapper;
import com.umbrella.worker.dao.WSmsRecordMapper;
import com.umbrella.worker.dao.WSupplierAccountMapper;
import com.umbrella.worker.dao.WSupplierAccountRecordMapper;
import com.umbrella.worker.dao.WSupplierMapper;
import com.umbrella.worker.dao.WSupplierPayrecordMapper;
import com.umbrella.worker.dto.AccountRecordDO;
import com.umbrella.worker.dto.CountAccount;
import com.umbrella.worker.dto.SupplierAccountDO;
import com.umbrella.worker.dto.SupplierAccountRecordDO;
import com.umbrella.worker.dto.SupplierDO;
import com.umbrella.worker.dto.SupplierPayrecordDO;
import com.umbrella.worker.entity.WSupplier;
import com.umbrella.worker.entity.WSupplierAccount;
import com.umbrella.worker.entity.WSupplierAccountExample;
import com.umbrella.worker.entity.WSupplierAccountRecord;
import com.umbrella.worker.entity.WSupplierAccountRecordExample;
import com.umbrella.worker.entity.WSupplierExample;
import com.umbrella.worker.entity.WSupplierPayrecord;
import com.umbrella.worker.entity.WSupplierPayrecordExample;
import com.umbrella.worker.query.SupplierAccountQuery;
import com.umbrella.worker.query.SupplierAccountRecordQuery;
import com.umbrella.worker.query.SupplierPayrecordQuery;
import com.umbrella.worker.query.SupplierQuery;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.ISupplierAccountService;
import com.umbrella.worker.service.ISuppliersService;
import com.umbrella.worker.util.BeanUtilsExtends;
import com.umbrella.worker.util.StringUtil;
@Service("suppliersService")
public class SuppliersServiceImpl  extends BaseServiceImpl implements ISuppliersService {
	
	private static Logger logger = Logger.getLogger(SuppliersServiceImpl.class);
	@Autowired
	private WSupplierMapper supplierMapper;
	@Autowired
	private WSupplierAccountMapper supplierAccountMapper;
	@Autowired
	private WSupplierPayrecordMapper supplierPayrecordMapper;
	@Autowired
	private WOrderMapper orderMapper;
	@Autowired
	private WSmsRecordMapper smsRecordMapper;
	@Autowired
	private WAccountRecordMapper accountRecordMapper;
	@Autowired
	private WSupplierAccountRecordMapper supplierAccountRecordMapper;
	
	@Autowired
	private ISupplierAccountService supplierAccountService;
	
	
	@Transactional
	@Override
	public ResultDO create(SupplierDO supplierDO) {
		
		WSupplier supplier = new WSupplier();
		
		ResultSupport result = BeanUtilsExtends.copy(supplier, supplierDO);
		
		if(!result.isSuccess()) {
			return result;
		}
		
		int recordNum = -1;
		
		supplier.setDatalevel(1);
		supplier.setwSMoney(3000);//黑币
		//状态码 0：停用 1：启用  2:待审核 3 不通过 4 通过
		supplier.setStatus(2);
		supplier.setwSGrade(1);
		supplier.setCreateTime(Calendar.getInstance().getTime());
		supplier.setModifiTime(Calendar.getInstance().getTime());
		
		try {
			recordNum = supplierMapper.insertSelective(supplier);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			logger.error("[obj:supplier][opt:create][msg:" + e.getMessage()
			+ "]");
			return result;
		}
		
		WSupplierAccount supplierAccount = new WSupplierAccount();
		supplierAccount.setId(supplier.getId());
		supplierAccount.setwAName(supplierDO.getwSName());
		supplierAccount.setCreateAuthor(supplierDO.getCreateAuthor());
		supplierAccount.setCreateTime(Calendar.getInstance().getTime());
		supplierAccount.setModifiAuthor(supplierAccount.getCreateAuthor());
		supplierAccount.setModifiTime(supplierAccount.getCreateTime());
		supplierAccount.setStatus(1);
		supplierAccount.setDatalevel(1);
		
		try {
			recordNum = supplierAccountMapper.insertSelective(supplierAccount);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			logger.error("[obj:supplier][opt:create][msg:" + e.getMessage()
			+ "]");
			return result;
		}
		
		if(recordNum == 1) {
			result.setModel(ResultDO.FIRST_MODEL_KEY, supplier.getId());
		} else {
			result.setSuccess(false);
		}
		return result;
	}
	@Transactional
	@Override
	public ResultDO modifi(SupplierDO supplierDO) {
		
		WSupplier supplier = new WSupplier();

		ResultSupport result = BeanUtilsExtends.copy(supplier, supplierDO);
		// 拷贝失败
		if (!result.isSuccess()) {
			return result;
		}
		supplier.setModifiTime(Calendar.getInstance().getTime());
		int recordNum = -1;
		try {
			recordNum = supplierMapper.updateByPrimaryKeySelective(supplier);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:supplier][opt:modifi][msg:" + e.getMessage()
					+ "]");
			return result;
		}
		if (recordNum < 1) {
			result.setSuccess(false);
		}

		return result;
	}
	@Transactional
	@Override
	public ResultDO remove(int supplierId) {
		
		ResultSupport result = new ResultSupport();
		
		WSupplier supplier = new WSupplier();
		
		if(StringUtil.isGreatOne(supplierId)) {
			supplier.setId(supplierId);
		} else {
		    result.setSuccess(false);
		    return result;
		}
		supplier.setDatalevel(-1);
		int recordNum = -1;
		try {
			recordNum = supplierMapper.updateByPrimaryKeySelective(supplier);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:remove][msg:"+e.getMessage()+"]");
	        return result;
		}
		if (recordNum != 1) {
			result.setSuccess(false);
		}
		return result;
	}
	
	
	@Override
	public ResultDO get(int supplierId) {
		
		ResultSupport result = new ResultSupport();
		
		WSupplier supplier = null;
		if(!StringUtil.isGreatOne(supplierId)) {
			 result.setSuccess(false);
			 return result;
		} 
		
		try {
			supplier = supplierMapper.selectByPrimaryKey(supplierId);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		
		SupplierDO supplierDO = getSupplierDO(supplier);
		if(supplierDO != null) {
			result.setModel(ResultSupport.FIRST_MODEL_KEY, supplierDO);
		} else {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			return result;
		}
		
		return result;
	}
	
	public ResultDO getAccount(Integer id) {
		
		ResultSupport result = new ResultSupport();
		
		WSupplierAccount supplierAccount = null;
		if(!StringUtil.isGreatOne(id)) {
			 result.setSuccess(false);
			 return result;
		} 
		BigDecimal cash = new BigDecimal(0.00);
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-mm-DD HH:MM:ss");
			String endDate2 = dateFormat.format(new Date(System.currentTimeMillis() * 7 * 24 * 60 * 60 * 1000));
			supplierAccount = supplierAccountMapper.selectByPrimaryKey(id);
			cash = orderMapper.orderWithdrawCash(id, endDate2);
		
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		
		SupplierAccountDO supplierAccountDO = getSupplierAccountDO(supplierAccount);
		
		if(supplierAccountDO != null) {
			if(cash != null) {
				supplierAccountDO.setPay(cash);
			} else {
				supplierAccountDO.setPay(new BigDecimal(0.00));
			}
			result.setModel(ResultSupport.FIRST_MODEL_KEY, supplierAccountDO);
		} else {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			return result;
		}
		
		return result;
	}
	
	public ResultDO getCountAccount() {
		
		ResultSupport result = new ResultSupport();
		
		CountAccount countAccount = new CountAccount();
		BigDecimal fee = new BigDecimal(0.00);
		BigDecimal rePay = new BigDecimal(0.00);
		BigDecimal pay = new BigDecimal(0.00);
		BigDecimal waitSettle = new BigDecimal(0.00);
		BigDecimal endSettle = new BigDecimal(0.00);
		try {
			fee = orderMapper.selectOrderFeeStatistics();
			waitSettle = orderMapper.selectWaitSettle();
			endSettle = orderMapper.selectEndSettle();
			if(fee != null) {
				countAccount.setOrderTotal(fee);
			} else {
				countAccount.setOrderTotal(new BigDecimal(0.00));
			}
			if(waitSettle != null) {
				countAccount.setWaitSettle(waitSettle);
			} else {
				countAccount.setWaitSettle(new BigDecimal(0.00));
			}
			if(endSettle != null) {
				countAccount.setEndSettle(endSettle);
			} else {
				countAccount.setEndSettle(new BigDecimal(0.00));
			}
			
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		try {
			rePay = supplierPayrecordMapper.supplierRePayCount();
			pay = supplierPayrecordMapper.supplierPayCount();
			if(rePay != null) {
				countAccount.setApplyAmount(rePay);
			} else {
				countAccount.setApplyAmount(new BigDecimal(0.00));
			}
			if(pay != null) {
				countAccount.setPaidAmount(pay);
			} else {
				countAccount.setPaidAmount(new BigDecimal(0.00));
			}
			
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		try {
			BigDecimal smsFeeCount = smsRecordMapper.selectAllSmsFeeCount();
			if(smsFeeCount != null) {
				countAccount.setSmsFee(smsFeeCount);
			} else {
				countAccount.setSmsFee(new BigDecimal(0.00));
			}
			
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		BigDecimal amount = new BigDecimal(0.00);
		fee = countAccount.getOrderTotal();
		pay = countAccount.getPaidAmount();
		if(fee.compareTo(BigDecimal.ZERO) != 0 ) {
			amount = fee.subtract(pay);
		} else {
			amount = fee;
		}
		countAccount.setAmount(amount);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, countAccount);
		return result;
	}

	@Override
	public ResultDO list(SupplierQuery supplierQuery) {
		
		ResultSupport result = new ResultSupport();
		
		WSupplierExample example = new WSupplierExample();
		WSupplierExample.Criteria c = example.createCriteria();
		
		if(StringUtil.isNotEmpty(supplierQuery.getName())) {
			c.andWSNameLike("%" + supplierQuery.getName() + "%");
		}
		
		if(StringUtil.isGreatOne(supplierQuery.getStatus())) {
			c.andStatusEqualTo(supplierQuery.getStatus());
			
		}
		
		c.andDatalevelEqualTo(1);
		
		if(StringUtil.isNotEmpty(supplierQuery.getOrderByClause())) {	
			example.setOrderByClause(" " + supplierQuery.getOrderByClause() + " " + supplierQuery.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME ASC");
		}
		
		if(supplierQuery.isPage()) {
			long count = 0;
			try {
				count = supplierMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:member][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = supplierQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			
			String pageByClause = " limit " + (pageNO * supplierQuery.getPageRows())
					+ "," + supplierQuery.getPageRows();
			
			example.setPageByClause(pageByClause);
		}
		
		List<WSupplier> list = null;
		
		try {
			list = supplierMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<SupplierDO> supplierList = getSupplierDOList(list);

		result.setModel(ResultSupport.FIRST_MODEL_KEY, supplierList);
		
		return result;
	}
	
	@Override
	public ResultDO listPayrecord(SupplierPayrecordQuery query) {
		ResultSupport result = new ResultSupport();
	
		
		WSupplierPayrecordExample example = new WSupplierPayrecordExample();
		WSupplierPayrecordExample.Criteria c = example.createCriteria();
		if(StringUtil.isGreatOne(query.getSupplierId())) {
			c.andWPSupperIdEqualTo(query.getSupplierId());
		}
		if(StringUtil.isGreatOne(query.getStatus())) {
			c.andStatusEqualTo(query.getStatus());
		}
		c.andDatalevelEqualTo(1);
		
		if(StringUtil.isNotEmpty(query.getOrderByClause())) {	
			example.setOrderByClause(" " + query.getOrderByClause() + " " + query.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME DESC");
		}
		
		if(query.isPage()) {
			long count = 0;
			try {
				count = supplierPayrecordMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:member][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = query.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			
			String pageByClause = " limit " + (pageNO * query.getPageRows())
					+ "," + query.getPageRows();
			
			example.setPageByClause(pageByClause);
		}
		
		List<WSupplierPayrecord> list = null;
		
		try {
			list = supplierPayrecordMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<SupplierPayrecordDO> supplierList = getSupplierPayrecordDOList(list);

		List<SupplierPayrecordDO> list2 = new ArrayList<SupplierPayrecordDO>();
		if(supplierList != null) {
			for(SupplierPayrecordDO supplierPayrecordDO : supplierList) {
				try {
					WSupplier supplier = supplierMapper.selectByPrimaryKey(supplierPayrecordDO.getwPSupperId());
					supplierPayrecordDO.setSupplierName(supplier.getwSName());
					list2.add(supplierPayrecordDO);
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
			}
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list2);
		return result;
	}
	
	
	@Override
	public ResultDO listSArecord(SupplierAccountRecordQuery query) {
		ResultSupport result = new ResultSupport();
	
		
		WSupplierAccountRecordExample example = new WSupplierAccountRecordExample();
		WSupplierAccountRecordExample.Criteria c = example.createCriteria();
		if(StringUtil.isGreatOne(query.getwSupplierId())) {
			c.andWSupplierIdEqualTo(query.getwSupplierId());
		}
		c.andDatalevelEqualTo(1);
		
		if(StringUtil.isNotEmpty(query.getOrderByClause())) {	
			example.setOrderByClause(" " + query.getOrderByClause() + " " + query.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME ASC");
		}
		
		if(query.isPage()) {
			long count = 0;
			try {
				count = supplierAccountRecordMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:supplierAccountRecord][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = query.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			
			String pageByClause = " limit " + (pageNO * query.getPageRows())
					+ "," + query.getPageRows();
			
			example.setPageByClause(pageByClause);
		}
		
		List<WSupplierAccountRecord> list = null;
		
		try {
			list = supplierAccountRecordMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplierAccountRecord][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<SupplierAccountRecordDO> supplierList = getSupplierAccountRecordDOList(list);

		List<SupplierAccountRecordDO> list2 = new ArrayList<SupplierAccountRecordDO>();
		if(supplierList != null) {
			for(SupplierAccountRecordDO supplierAccountRecordDO : supplierList) {
				try {
					WSupplier supplier = supplierMapper.selectByPrimaryKey(supplierAccountRecordDO.getwSupplierId());
					supplierAccountRecordDO.setwSupplierName(supplier.getwSName());
					list2.add(supplierAccountRecordDO);
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
			}
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list2);
		
		return result;
	}
	
	@Transactional
	public ResultDO payStatus(Integer payrecordId) {
		WSupplierPayrecord supplierPayrecord = new WSupplierPayrecord();
		supplierPayrecord.setStatus(2);
		supplierPayrecord.setId(payrecordId);
		ResultSupport result = new ResultSupport();
		int recordNum = -1;
		try {
			recordNum = supplierPayrecordMapper.updateByPrimaryKeySelective(supplierPayrecord);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			logger.error("[obj:supplier][opt:create][msg:" + e.getMessage()
			+ "]");
			return result;
		}
		
		if(recordNum != 1) {
			result.setSuccess(false);
		}
		
		WSupplierPayrecord wsp = supplierPayrecordMapper.selectByPrimaryKey(payrecordId);
		
		AccountRecordDO accountRecordDO = new AccountRecordDO();
		accountRecordDO.setwType(2);
		accountRecordDO.setwItem("支付渠道商");
		accountRecordDO.setwMoney(wsp.getwPAmount());
		accountRecordDO.setwDesc("支付渠道商提现");
		accountRecordDO.setModifiAuthor("system");
		accountRecordDO.setCreateTime(Calendar.getInstance().getTime());
		accountRecordDO.setCreateAuthor("system");
		accountRecordDO.setStatus(1);
		accountRecordDO.setDatalevel(1);
		supplierAccountService.createRecrod(accountRecordDO);
		return result;
	}

	@Transactional
	public ResultDO pay(SupplierPayrecordDO supplierPayrecordDO) {
		WSupplierPayrecord supplierPayrecord = new WSupplierPayrecord();
		
		ResultSupport result = BeanUtilsExtends.copy(supplierPayrecord, supplierPayrecordDO);
		
		if(!result.isSuccess()) {
			return result;
		}
		
		int recordNum = -1;
		
		supplierPayrecord.setDatalevel(1);
		supplierPayrecord.setStatus(1);
		
		supplierPayrecord.setCreateTime(Calendar.getInstance().getTime());
		supplierPayrecord.setModifiTime(Calendar.getInstance().getTime());
		
		try {
			recordNum = supplierPayrecordMapper.insertSelective(supplierPayrecord);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			logger.error("[obj:supplier][opt:create][msg:" + e.getMessage()
			+ "]");
			return result;
		}
		
		if(recordNum == 1) {
			result.setModel(ResultDO.FIRST_MODEL_KEY, supplierPayrecord.getId());
		} else {
			result.setSuccess(false);
		}
		return result;
	}
	
	protected List<SupplierAccountRecordDO> getSupplierAccountRecordDOList(List<WSupplierAccountRecord> list) {
		List<SupplierAccountRecordDO> resultList = new ArrayList<SupplierAccountRecordDO>();
		if(list.size() > 0) {
			for(WSupplierAccountRecord supplierAccountrecord : list) {
				SupplierAccountRecordDO supplierAccountRecordDO = this.getSupplierAccountRecordDO(supplierAccountrecord);
				if(supplierAccountRecordDO != null) {
					resultList.add(supplierAccountRecordDO);
				} else {
					return null;
				}
			}
		} else {
			return null;
		}
		return resultList;
	}
	
	protected SupplierAccountRecordDO getSupplierAccountRecordDO(WSupplierAccountRecord obj) {
		if(obj == null) return null;
		SupplierAccountRecordDO dst = new SupplierAccountRecordDO();
		return BeanUtilsExtends.copyProperties(dst, obj) ? dst : null;
	}
	
	
	@Override
	public ResultDO listAccount(SupplierAccountQuery query) {

		ResultSupport result = new ResultSupport();
		
		WSupplierAccountExample example = new WSupplierAccountExample();
		WSupplierAccountExample.Criteria c = example.createCriteria();
		
		if(StringUtil.isNotEmpty(query.getSupplierName())) {
			c.andWANameLike("%" + query.getSupplierName() + "%");
		}
		
		c.andDatalevelEqualTo(1);
		
		if(StringUtil.isNotEmpty(query.getOrderByClause())) {	
			example.setOrderByClause(" " + query.getOrderByClause() + " " + query.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME ASC");
		}
		
		if(query.isPage()) {
			long count = 0;
			try {
				count = supplierAccountMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:member][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = query.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			
			String pageByClause = " limit " + (pageNO * query.getPageRows())
					+ "," + query.getPageRows();
			
			example.setPageByClause(pageByClause);
		}
		
		List<WSupplierAccount> list = null;
		
		try {
			list = supplierAccountMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<SupplierAccountDO> supplierList =getSupplierAccountDOList(list);

		List<SupplierAccountDO> account =new ArrayList<SupplierAccountDO> ();
		
		if(supplierList!=null){
			for (SupplierAccountDO supplierAccountDO : supplierList) {
				account.add(supplierAccountDO);
			}
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, account);
		
		return result;
	}

	
	/**
	 * 更新账户余额
	 */
	@Override
	public ResultDO updateSupplierAccount(Integer payrecordId){
		ResultSupport result = new ResultSupport();
		int recordNum = -1;
		
		WSupplierPayrecord supplierPayrecord =null;
		try {
			supplierPayrecord =supplierPayrecordMapper.selectByPrimaryKey(payrecordId);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		
		if(supplierPayrecord!=null){
			//提现金额
			BigDecimal bal=supplierPayrecord.getwPAmount();
			//手续费
			BigDecimal fee=supplierPayrecord.getwPFee();
			WSupplierAccount account = null;
			
			try {
				account = supplierAccountMapper.selectByPrimaryKey(supplierPayrecord.getwPSupperId());
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			
			if(account!=null){
				BigDecimal balance = new BigDecimal(0.0);
				if(account.getwABalance()!=null){
					balance=account.getwABalance();
				}
				
				balance = balance.subtract(bal);
				balance=balance.subtract(fee);
				account.setwABalance(balance);
				BigDecimal wfee=new BigDecimal(0.0);
				if(account.getwAFee()!=null){
					wfee=account.getwAFee();
				}
				
				wfee=wfee.add(fee);
				account.setwAFee(wfee);
				
				BigDecimal raw=bal.subtract(fee);
				account.setwALastWithdraw(raw);
				account.setwALastWithdrawDate(Calendar.getInstance().getTime());
				
				try {
					recordNum=supplierAccountMapper.updateByPrimaryKeySelective(account);
				} catch (Exception e) {
					result.setSuccess(false);
			        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
			        return result;
				}
				
			}else{
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        return result;
			}
			
		}else{
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        return result;
		}
		
		if(recordNum != 1) {
			result.setSuccess(false);
		}
		return result;
	}
	
}
